// pages/clickMe/index.js
import api from '../../api/index'
import { AppId } from '../../api/config'
import { userInfoStore } from '../../store/userInfo'
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLevel: 0,
    userPhone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow: function () {
    let p = userInfoStore.userInfo.phone;
    this.setData(
      {
        userLevel: userInfoStore.userInfo.level,
        userPhone: p ? (p.substr(0, 3) + '****' + p.substr(7)) : '***********'
      });

    let giftBag = this.selectComponent("#giftbag");
    app.displayGiftBag(giftBag);

    // if (app.globalData.clickMeNav && app.globalData.clickMeNav == 'coupon') {
    //   app.globalData.clickMeNav = null;
    //   wx.navigateTo({
    //     url: 'coupon/index?type=0',
    //   })
    // }
  },

  getPhoneNumber: function (e) {
    // console.log(e);
    if (e.detail.iv) {
      let { session_key } = userInfoStore.userInfo;
      let pc = new WXBizDataCrypt(AppId, session_key);
      var ret = pc.decryptData(e.detail.encryptedData, e.detail.iv)
      console.log('解密后 data: ', ret)

      var param = {
        openId: userInfoStore.userInfo.openId,
        phone: ret.phoneNumber,
        level: 1
      }
      if (app.globalData.inviteCode)
        param = Object.assign(param, { recomendCode: app.globalData.inviteCode })
      var self = this
      api.saveUser(param).then(res => {
        let { userInfo } = userInfoStore;
        let { result } = res;
        result.session_key = userInfo.session_key;
        userInfoStore.updateUserInfo(result);
        wx.showModal({
          title: "提交成功",
          showCancel: false,
          confirmText: "我知道了",
          success: function (res) {
            if (res.confirm) {
              self.setData({
                userLevel: userInfoStore.userInfo.level
              });
            }
          }
        })
      }, err => {
        wx.showModal({
          title: "提交失败",
          content: err,
          showCancel: false,
          confirmText: "我知道了"
        })
      })

    }
  },

  showDisign:function(e){
    wx.navigateTo({
      url: 'plan/index',
    })
    // wx.showToast({
    //   icon: "none",
    //   title: '敬请期待...',
    // });  
    // wx.navigateTo({
    //   url: '../webView/index?url=' + encodeURIComponent("https://www.daikin1995.com/contractcallback.html"),
    // })
  },

  showdefault:function(e){
    if(this.data.userLevel==0){
      wx.navigateTo({
        url: '../liuzi/liuzi',
      })
    }else{
      let r = e.currentTarget.dataset.row;
      let t = e.currentTarget.dataset.type;
      if (r == 1) {
        wx.navigateTo({
          url: 'order?status=' + t,
        })
      } else if (r == 2 && t){
        wx.navigateTo({
          url: 'coupon/index?type=' + t,
        })
      } else if (r == 3 && t){
        
      } else {
        wx.showToast({
          icon: "none",
          title: '敬请期待...',
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().share();
  }
})